var commonView = require("./commonView.js");
//var echarts = require('echarts');

// 查询识别
var $setMealSpan4Box, $setMealButton, $identifyResultBox, $localLoading, $userId, $bankCard, $userPhone, $userIdBox;
var spinner, pkId, seralNo;
var infoFormValid;
var step = 0;
var $Validform;
// 人脸识别 值对应码
var errorCodeMessage = {
    "-4006": "视频中自拍照特征提取失败",
    "-4007": "视频中自拍照之间对比失败",
    "-4009": "Card照片提取特征失败",
    "-4010": "自拍照与Card照片相似度计算失败",
    "-4015": "自拍照人脸检测失败",
    "-4016": "自拍照解码失败",
    "-4017": "Card照片人脸检测失败",
    "-5001": "Card照片解码失败",
    "-5002": "唇语失败",
    "-5005": "自拍照解析照片不足",
    "-5007": "视频没有声音",
    "-5008": "声音识别失败",
    "-5009": "视频人脸检测失败，没有嘴或者脸",
    "-5010": "唇动失败",
    "-5011": "活体检测失败--不是活体",
    "-5012": "视频中噪声太大",
    "-5013": "视频里的声音太小",
    "-5014": "活体检测level参数无效",
    "-5015": "视频像素太低，最小320*480",
    "-5016": "视频里的人不是活体（疑似翻拍视频等攻击）",
    "-5801": "请求缺少身份证号码或身份证姓名",
    "-5802": "服务器内部错误，服务暂时不可用，请稍后重试",
    "-5803": "身份证姓名与身份证号码不一致",
    "-5804": "身份证号码无效",
    "-5805": "用户未输入图像或者 url 下载失败",
    "-5806": "身份证号码或者身份证姓名格式错误",
    "-5807": "查询身份证信息失败",
    "-9999": "视频验证人与身份证本人相似度低于50%"

};

var scoreMap = [{
    "id": 5,
    "value1": "800",
    "valueType": "信用分",
    "value2": "1000",
    "valueName": "极好",
    "message": "[800,1000)"
}, {
    "id": 4,
    "value1": "700",
    "valueType": "信用分",
    "value2": "800",
    "valueName": "优秀",
    "message": "[700,800)"
}, {
    "id": 3,
    "value1": "600",
    "valueType": "信用分",
    "value2": "700",
    "valueName": "良好",
    "message": "[600,700)"
}, {
    "id": 2,
    "value1": "500",
    "valueType": "信用分",
    "value2": "600",
    "valueName": "一般",
    "message": "[500,600)"
}, {
    "id": 1,
    "value1": "0",
    "valueType": "信用分",
    "value2": "500",
    "valueName": "较差",
    "message": "[0,500)"
}];
// 查询结果对应表
var identifyMap = [{
    "value": "basic", // 和后台返回的属性名一致(基本信息) 0
    "name": "基本信息",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "查询时间",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "查询组合",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "未查得数据"
    }]
}, {
    "value": "bank", // 和后台返回的属性名一致(基本信息) 1
    "name": "银行卡查询",
    "data": [{
        "title": "姓名",
        "value": "西恩"
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "银行卡",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "一致"
    }]
}, {
    "value": "blacklist", // 和后台返回的属性名一致(基本信息) 2
    "name": "不良信息查询",
    "data": [{
        "title": "姓名",
        "value": "西恩"
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "没有不良信息"
    }]
}, {
    "value": "phoneblacklist", // 和后台返回的属性名一致(基本信息) 3
    "name": "电话黑名单查询",
    "data": [{
        "title": "姓名",
        "value": "西恩"
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "allInPay", // 和后台返回的属性名一致(基本信息) 4
    "name": "通联信用分",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "银行卡",
        "value": ""
    }, {
        "title": "信用分",
        "value": ""
    }, {
        "title": "信用评价",
        "value": "危险"
    }]
}, {
    "value": "phonecredit", // 和后台返回的属性名一致(基本信息) 5
    "name": "手机号信用核查",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "multiterm", // 和后台返回的属性名一致(多项身份信息) 6
    "name": "多项身份信息",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "overdue", // 和后台返回的属性名一致(逾期信息) 7
    "name": "逾期信息",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "Loaninfo", // 和后台返回的属性名一致(贷款信息) 8
    "name": "贷款信息",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "consume", // 和后台返回的属性名一致(个人消费特征信息) 9
    "name": "个人消费特征信息",
    "data": [{
        "title": "姓名",
        "value": ""
    }, {
        "title": "身份证号",
        "value": ""
    }, {
        "title": "手机号",
        "value": ""
    }, {
        "title": "查询结果",
        "value": "查得"
    }]
}, {
    "value": "creditscore", // 和后台返回的属性名一致(商务信用分) 10
    "name": "商务信用分",
    "data": [{
        "title": "商务信用分(0-1000)",
        "value": ""
    }, {
        "title": "",
        "value": ""
    }]
}];

// 填充名字身份证手机
function fillInfo() {
    var inputUserName = $("#inputUserName").val();
    var userId = $userId.val();
    var userPhone = $("#user-phone").val();

    for (var i = 0, l = identifyMap.length; i < l; i++) {
        for (var n = 0, m = identifyMap[i].data.length; n < m; n++) {
            if (identifyMap[i].data[n].title == "姓名") {
                identifyMap[i].data[n].value = inputUserName;
            }
            if (identifyMap[i].data[n].title == "身份证号") {
                identifyMap[i].data[n].value = userId;
            }
            if (identifyMap[i].data[n].title == "手机号") {
                identifyMap[i].data[n].value = userPhone;
            }
        }

    }
}

// 选择查询组合
function selectSetMeal() {
    pkId = $(this).data("pkid");
    $setMealSpan4Box.find("i.icon-ok").removeClass("icon-ok");
    $setMealSpan4Box.removeClass("checked");
    $(this).addClass("checked");
    $(this).find(".box-header i").addClass("icon-ok");
    addDelteBankInput();
    commonView.showStep("verify1");
    $Validform.resetForm();
    $("#id-dynamic_tips").text("");
    if ($("#card-dynamic_tips")[0]) {
        $("#card-dynamic_tips").text("");
    }

    $("#phone-dynamic_tips").text("");
}

// 设置组合pkId
function setPkId(pkid) {
    pkId = pkid;
}


// 初始化变量
function initIdentifyVal() {
    $setMealSpan4Box = $("#set-meal_box>.span4.box");
    $setMealButton = $("#set-meal_box button");
    $identifyResultBox = $("#identity-result_box");
    $localLoading = $("#local-loading");
    $userId = $("#user-id");
    $userIdBox = $("#user-id_box");
    $bankCard = $("#bank-card");
    $userPhone = $("#user-phone");
    $Validform = $("#query-info_form").Validform({
        tiptype: 2,
        datatype: {
            "zh2-10": /^[\u4E00-\u9FA5\uf900-\ufa2d]{2,10}$/,
            "sfz": /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/,
            "yxq": /([0][1-9]|[1][0-2])\d{2}/
        }
    });
    spinner = new Spinner().spin();
    $.Tipmsg.r = "";
}

// 请求查询
function queryIdentify(interId) {
    var inputUserName = $("#inputUserName").val();
    var userId = $userId.val();
    var bankNo = $("#bank-card").val();
    var userPhone = $("#user-phone").val();
    if (step == 0 || !seralNo) {
        seralNo = "342648762343" + "_" + pkId + "_" + (new Date()).getTime();
    }
    var paraments = {
        pkId: pkId,
        merId: window.merId,
        merName: '测试机构',
        operatorId: '18616961612',
        name: inputUserName,
        idCard: userId,
        phoneNo: userPhone,
        bankNo: bankNo,
        step: step,
        interId: interId,
        seralNo: seralNo
    };
    if (infoFormValid.check()) {
        $.post(queryIdentifyServlet, paraments, querySuccess);
    } else {
        $('#loding').modal('hide');
    }

}

// 提示函数
function promtmsg(pmsg) {
    $("#promt-body").text(pmsg);
    $("#prompt-modal").modal("show");
}

// 查询回调函数
function querySuccess(queryData) {

    commonView.needLoading(queryData);
    // console.dir(queryData);
    $('#loding').modal('hide');
    if (queryData.code == "0012" || queryData.code == "0131") {
        if (queryData.msg) {
            $("#promt-body").text(queryData.msg);
        } else {
            $("#promt-body").text("当前用户没有查询权限。");
        }

        $("#prompt-modal").modal("show");

        return;
    }
    commonView.showStep("verify2");
    $("#identify-error").hide();

    if (step == 0) {
        //        $identifyResultBox.empty();
        resetIndentify();
        $identifyResultBox.show();
    }
    var selectDate = commonView.getDateFormate("yyyy-mm-dd hh:ii:ss", new Date());
    processingData(queryData, $identifyResultBox, selectDate);
    if (queryData.next == "1") {
        setLocalLoading("下个结果查询");
        step++;
        queryIdentify(queryData.interId);
    } else {
        step = 0;

    }

}

// 判断是否为数组
function isArray(o) {
    return Object.prototype.toString.call(o) == '[object Array]';
}

// 绘制雷达图
function createChart(seriesValue, graphictext) {
    var myChart = echarts.init(document.getElementById('credit_chart'));
    var myChart2 = echarts.init(document.getElementById('credit_chart2'));
    var areaColor = ['#177ae4','#62acfc'];
    if(parseInt(graphictext) < 500){
        areaColor = ['#f34541','#fc8381'];        
    }else if(parseInt(graphictext) < 600){
        areaColor = ['#f89e1a','#f3b65c'];
    }else if(parseInt(graphictext) < 700){
        areaColor = ['#4abf65','#89eba0'];
    }else if(parseInt(graphictext) < 800){
        areaColor = ['#00acec','#5bcffa'];
    }
    // 绘制图表
    myChart.setOption({
        title: {
            text: ''
        },
        tooltip: {
            show: false
        },
        radar: {
            // shape: 'circle',
            indicator: [{
                name: '身份',
                max: 1000
            }, {
                name: '行为',
                max: 1000
            }, {
                name: '消费',
                max: 1000
            }, {
                name: '外部',
                max: 1000
            }, {
                name: '社交',
                max: 1000
            }],
            center: ['50%', '51%'],
            name: {
                formatter: '{value}',
                textStyle: {
                    color: '#2792e4'
                }
            },
            splitArea: {
                areaStyle: {
                    color: ['#fff', '#fff', '#fff', '#fff']
                }
            },
            axisLine: {
                lineStyle: {
                    color: 'rgba(63, 219, 220,0.8)'
                }
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(63, 219, 220, 0.8)'
                }
            }
        },
        graphic: [{
            type: 'text',
            z: 100,
            left: '132',
            top: '85',
            style: {
                fill: '#fff',
                text: graphictext,
                font: 'bold 28px Microsoft YaHei'
            }
        }],

        color: "#103b5d",
        series: [{
            name: '信用',
            type: 'radar',
            data: [{
                value: seriesValue,
                name: '',

                areaStyle: {
                    normal: {
                        opacity: 0.7,
                        color: new echarts.graphic.LinearGradient(0, 0.5, 1, 0.5, [{
                            color: areaColor[0],
                            offset: 0
                        }, {
                            color: areaColor[1],
                            offset: 1
                        }])
                    }
                }
            }]
        }]
    });

    // 绘制记录详情的5维图
    myChart2.setOption({
        title: {
            text: ''
        },
        tooltip: {
            show: false
        },
        radar: {
            // shape: 'circle',
            indicator: [{
                name: '身份',
                max: 1000
            }, {
                name: '行为',
                max: 1000
            }, {
                name: '消费',
                max: 1000
            }, {
                name: '外部',
                max: 1000
            }, {
                name: '社交',
                max: 1000
            }],
            center: ['50%', '51%'],
            name: {
                formatter: '{value}',
                textStyle: {
                    color: '#2792e4'
                }
            },
            splitArea: {
                areaStyle: {
                    color: ['#fff', '#fff', '#fff', '#fff']
                }
            },
            axisLine: {
                lineStyle: {
                    color: 'rgba(63, 219, 220,0.8)'
                }
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(63, 219, 220, 0.8)'
                }
            }
        },
        graphic: [{
            type: 'text',
            z: 100,
            left: '132',
            top: '85',
            style: {
                fill: '#fff',
                text: graphictext,
                font: 'bold 28px Microsoft YaHei'
            }
        }],

        color: "#103b5d",
        series: [{
            name: '信用',
            type: 'radar',
            data: [{
                value: seriesValue,
                name: '',

                areaStyle: {
                    normal: {
                        opacity: 0.7,
                        color: new echarts.graphic.LinearGradient(0, 0.5, 1, 0.5, [{
                            color: areaColor[0],
                            offset: 0
                        }, {
                            color: areaColor[1],
                            offset: 1
                        }])
                    }
                }
            }]
        }]
    });
}


// 数据处理
function processingData(queryData, $tparent, selectDate) {
    var dangerClass = {
        index: -1,
        className: "",
        theName: ""
    };
    var createTabelData, createTableTitle;
    var trData;
    if (queryData.code != "0000") {
        step = 0;
        dangerClass.className = "label-danger";
        $localLoading.hide();
    }
    if (queryData.funcId == "008") { // 商务信用分
        var seriesValue = [0, 0, 0, 0, 0];
        var graphictext = 0;
        if (queryData.code == "0000") {
            $(".fourth-identity-box").show();

            if (queryData.verify && queryData.verify != "" && queryData.verify != "null" && queryData.verify != null && queryData.verify != "undefined") {
                var xypj = "";
                $(".third-identity-box").show();
                var theVerify = JSON.parse(queryData.verify);

                if (isArray(theVerify)) {
                    for (var vi = 0, vl = theVerify.length; vi < vl; vi++) {
                        if (theVerify[vi][0] == "id_info") {
                            seriesValue[0] = theVerify[vi][1];
                        } else if (theVerify[vi][0] == "behaviorial_character") {
                            seriesValue[1] = theVerify[vi][1];
                        } else if (theVerify[vi][0] == "consumption_data") {
                            seriesValue[2] = theVerify[vi][1];
                        } else if (theVerify[vi][0] == "outsider_score") {
                            seriesValue[3] = theVerify[vi][1];
                        } else if (theVerify[vi][0] == "social_data") {
                            seriesValue[4] = theVerify[vi][1];
                        } else if (theVerify[vi][0] == "wjt_credit") {
                            graphictext = theVerify[vi][1];
                        }
                    }
                }

                if (parseInt(graphictext) < 500) {
                    $(".rate-jiaocha").attr("src", "./img/jiaocha_icon2.png");
                } else if (parseInt(graphictext) < 600) {
                    $(".rate-yiban").attr("src", "./img/yiban_icon2.png");
                } else if (parseInt(graphictext) < 700) {
                    $(".rate-lianghao").attr("src", "./img/lianghao_icon2.png");
                } else if (parseInt(graphictext) < 800) {
                    $(".rate-youxiu").attr("src", "./img/youxiu_icon2.png");
                } else {
                    $(".rate-jihao").attr("src", "./img/jihao_icon2.png");
                }
                if (queryData.verifyMsg && queryData.verifyMsg != "" && queryData.verifyMsg != "null" && queryData.verifyMsg != null) {
                    var thevmsg = JSON.parse(queryData.verifyMsg);

                    $(".indetity-dhzy h3.sjzt").text(thevmsg.telStatus);
                    $(".indetity-dhzy h3.pyyys").text(thevmsg.operator);
                    $(".indetity-dhzy h3.zwsc").text(thevmsg.isUseTime);


                    $(".indetity-yhk1 h3.jyyfs").text(thevmsg.MON_6_var1);
                    var cardType = ({
                        "1": "借记卡",
                        "2": "贷记卡",
                        "3": "预付费卡",
                        "4": "准贷记卡"
                    })[thevmsg.cardType];
                    if(thevmsg.LOC_6_var11 == "unknown"){
                        thevmsg.LOC_6_var11 = "未知";
                    }
                    $(".indetity-yhk1 h3.yhklx").text(cardType);
                    $(".indetity-yhk1 h3.cydq").text(thevmsg.LOC_6_var11);
                    $(".indetity-yhkjy h3.yjbs").text(thevmsg.RFM_6_var2);
                    $(".indetity-yhkjy h3.zdjyje").text(thevmsg.RFM_6_var3);
                    $(".indetity-yhkjy h3.pjje").text(thevmsg.RFM_6_var5);
                    $(".indetity-yhkjy2 h3.qxje").text(thevmsg.RFM_12_var30);
                    $(".indetity-yhkjy2 h3.kjjye").text(thevmsg.RFM_12_var40);
                    $(".indetity-yhkjy2 h3.zrje").text(thevmsg.RFM_12_var47);
                }

            } else {

                promtmsg("信用分查询失败");
            }
        } else {
            promtmsg("信用分查询失败");
        }
        createChart(seriesValue, graphictext);
    } else if (queryData.interId == "M005" && queryData.channelId == "suanhua") { // 手机号实名制比对结果
        $(".frist-identity-box").show();
        $(".indetity-time").text(selectDate);
        if (queryData.name && queryData.name != "null" && queryData.name != null && queryData.name != "undefined") {

            $(".identity-name").text(queryData.name);
        }
        if (queryData.idCard && queryData.idCard != null && queryData.idCard != "null" && queryData.idCard != "undefined") {
            $(".identity-userId").text(queryData.idCard);
        }
        if (queryData.phoneNo && queryData.phoneNo != null && queryData.phoneNo != "null" && queryData.phoneNo != "undefined") {
            $(".identity-phone").text(queryData.phoneNo);
        }
        if (pkId == "002") {
            $(".indentity-group").text("组合B");
            $(".identity-sf-title").text("身份手机号验证");
        } else if (pkId == "003") {
            $(".indentity-group").text("组合C");
            $(".identity-sf-title").text("身份手机号验证");
        } else if (pkId == "001") {
            $(".indentity-group").text("组合A");
            $(".identity-sf-title").text("身份验证结果");
            
        }
        if (queryData.code == "0000") {
            if (queryData.verify == "2") {
                $(".identity-sf").html('<i class="i-diff i-img"></i>不一致');
            } else if (queryData.verify == "1") {
                $(".identity-sf").html('<i class="i-yz i-img"></i>一致');
            }
        } else {
            $(".identity-sf").html('<i class="i-bad i-img"></i>查询失败');
            promtmsg("基本信息查询失败");
        }
    } else if (queryData.interId == "0001" && queryData.channelId == "kexin") { // 不良信息黑黑名单
        $(".identity-blxx-box").show();
        if (queryData.code == "0000") {
            if (queryData.data != "" && queryData.data != null && queryData.data != "null" && queryData.data != "undefined" && queryData.data != undefined) {
                if (isArray(queryData.data)) {
                    var av = joinArrayValue(queryData.data);
                    $(".identity-blxx").html('<i class="i-bad i-img"></i>' + av);
                } else if (queryData.data && queryData.data.value && queryData.data.value != "null" && queryData.data.value != null) {
                    $(".identity-blxx").html('<i class="i-bad i-img"></i>' + queryData.data.value);
                } else if (queryData.data != "null" && typeof queryData.data == "string") {
                    $(".identity-blxx").html('<i class="i-bad i-img"></i>' + queryData.data);
                } else {
                    $(".identity-blxx").html('<i class="i-good i-img"></i>没有不良信息');
                }
            } else {
                $(".identity-blxx").html('<i class="i-good i-img"></i>没有不良信息');
            }
        } else if (queryData.msg) {
            $(".identity-blxx").html('<i class="i-bad i-img"></i>' + queryData.msg);
        } else {
            $(".identity-blxx").html('<i class="i-bad i-img"></i>查询失败');
        }
    } else if (queryData.interId == "0001" && queryData.channelId == "tengxun") {
        // 优图视频人脸识别
        $(".frist-identity-box").show();
        $(".identity-rlbox").show();
        $(".indentity-group").text("组合A");
        $(".indetity-time").text(selectDate);
        $(".identity-sf-title").text("身份验证结果");
        if(queryData.code == "0000"){
            $(".identity-sf").html('<i class="i-yz i-img"></i>一致');
        }else{
            $(".identity-sf").html('<i class="i-diff i-img"></i>不一致');
        }
        var vfMsg = '';
        if (queryData.code == "0000") {
            if (queryData.verify == "0") {
                if (errorCodeMessage[queryData.verifyMsg]) {
                    vfMsg = '<i class="i-bad i-img"></i>' + errorCodeMessage[queryData.verifyMsg];
                } else if (parseInt(queryData.verifyMsg) > 50 && parseInt(queryData.verifyMsg) < 101) {
                    vfMsg = '<i class="i-good i-img"></i>查询通过(相似度' + queryData.verifyMsg + ')';
                } else {
                    vfMsg = '<i class="i-bad i-img"></i>非身份证本人';
                }
            }else{
                vfMsg = "人脸识别失败";
                if(queryData.verify == "-0001") {

                    var codeArray = queryData.verifyMsg.split(",");
                    if (codeArray[0] != "0") {
                        vfMsg = errorCodeMessage[codeArray[0]];
                    } else if (codeArray[1]) {
                        vfMsg = errorCodeMessage[codeArray[1]];
                    }
                }else {
                    if(errorCodeMessage[queryData.verify]){
                        vfMsg = errorCodeMessage[queryData.verify];
                    }
                }
            }
        }
        $(".identity-rl").html(vfMsg);
       
        var yzname = "";
        if (queryData.name && queryData.name != null && queryData.name != "null" && queryData.name != "undefined") {
            //            yzname = queryData.name;
            $(".identity-name").text(queryData.name);
        }
        var idcard = "";
        if (queryData.idCard && queryData.idCard != null && queryData.idCard != "null" && queryData.idCard != "undefined") {
            //            idcard = queryData.idCard;
            $(".identity-userId").text(queryData.idCard);
        }
        var phoneNo = "";
        if (queryData.phoneNo && queryData.phoneNo != null && queryData.phoneNo != "null" && queryData.phoneNo != "undefined") {
            //            phoneNo = queryData.phoneNo;
            $(".identity-phone").text(queryData.phoneNo);
        }
    } else if (queryData.interId == "8002" && queryData.channelId == "suanhua") { // 手机黑名单
        $(".second-identity-box").show();
        $(".identity-blacklist-box").show();
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                if (queryData.data && queryData.data != "" && queryData.data != null && queryData.data != "null" && queryData.data != "undefined") {
                    //                createTabelData = queryData.data;
                    if (isArray(queryData.data)) {
                        var av = joinArrayValue(queryData.data);
                        $(".identity-blacklist").html('<i class="i-bad i-img"></i>' + av);
                    } else if (queryData.data.value && queryData.data.value != null && queryData.data.value != "null") {
                        $(".identity-blacklist").html('<i class="i-bad i-img"></i>' + queryData.data.value)
                    }
                } else {
                    $(".identity-blacklist").html('<i class="i-yz i-img"></i>没有数据');
                }
                //        identifyMap[3].data[3].value = "查得";
            } else {
                $(".identity-blacklist").html('<i class="i-yz i-img"></i>未查得');
            }
        } else if (queryData.msg) {
            $(".identity-blacklist").html('<i class="i-diff i-img"></i>' + queryData.msg);
            promtmsg(queryData.msg);
        } else {
            dangerClass.index = 1;
            $(".identity-blacklist").html('<i class="i-diff i-img"></i>查询失败');
            promtmsg("手机号黑名单查询失败");
            //    identifyMap[3].data[3].value = "查询失败";
        }

    } else if (queryData.interId == "0002" && queryData.channelId == "tengxun") { // 腾讯反欺诈
        $(".second-identity-box").show();
        if (queryData.code == "0000") {

            if (queryData.verify && queryData.verify != null && queryData.verify != "null") {
                if (parseInt(queryData.verify) < 50) {
                    if (queryData.verifyMsg && queryData.verifyMsg != null && queryData.verifyMsg != "null") {
                        $(".indentity-fqz").html(queryData.verify + '(' + queryData.verifyMsg + ')');
                    } else {
                        $(".indentity-fqz").html(queryData.verify);
                    }

                } else {
                    if (queryData.verifyMsg && queryData.verifyMsg != null && queryData.verifyMsg != "null") {
                        $(".indentity-fqz").html(queryData.verify + '(' + queryData.verifyMsg + ')');
                    } else {
                        $(".indentity-fqz").html(queryData.verify);
                    }
                }
            } else {
                $(".indentity-fqz").html('<i class="i-bad i-img"></i>未查得');
            }
        } else {
            $(".indentity-fqz").html('<i class="i-bad i-img"></i>查询失败');
            promtmsg("反欺诈查询失败");
        }
    } else if (queryData.interId == "3012" && queryData.channelId == "suanhua") { // 手机信用查询      
        $(".fourth-identity-box").show();
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //                identifyMap[5].data[3].value = "查得";
                if (queryData.data && queryData.data != "") {
                    if (isArray(queryData.data)) {
                        for (var di = 0, dl = queryData.data; di < dl; di++) {
                            if (queryData.data[di].title == "运营商") {
                                $(".indetity-dhzy h3")[1].text(queryData.data[di].value);
                            } else if (queryData.data[di].title == "手机号码状态") {
                                $(".indetity-dhzy h3")[0].text(queryData.data[di].value);
                            } else if (queryData.data[di].title == "在网时长") {
                                $(".indetity-dhzy h3")[2].text(queryData.data[di].value);
                            }
                        }
                    }
                    //                    createTabelData = queryData.data;
                } else {
                    $(".indetity-dhzy h3").text("没有数据");
                }
            } else if (queryData.verify == "2") {
                $(".indetity-dhzy h3").text("未查得");
            } else if (queryData.verify == "31") {
                $(".indetity-dhzy h3").text("请求参数错误");
            } else if (queryData.verify == "5") {
                $(".indetity-dhzy h3").text("请求参数错误");
            } else {
                $(".indetity-dhzy h3").text("未查得");
            }
        } else if (queryData.msg) {
            $(".indetity-dhzy h3").text(queryData.msg);
        } else {
            dangerClass.index = 1;
            $(".indetity-dhzy h3").text("查询失败");
        }

    } else if (queryData.interId == "1003" && queryData.channelId == "suanhua") { // 手机信用查询    
        $(".identity-yhkbox").show();
        var sh1003Val = "比对失败";
        if (queryData.code == "0000") {

            if (queryData.verify == "1") {
                //                sh1003Val = "比对一致";
                $(".identity-sfyhk").html('<i class="i-yz i-img"></i>一致');
            } else if (queryData.verify == "2") {
                //                sh1003Val = "比对不一致";
                $(".identity-sfyhk").html('<i class="i-diff i-img"></i>不一致');
            } else if (queryData.verify == "31") {
                sh1003Val = "请求参数错误";
                $(".identity-sfyhk").html('<i class="i-bad i-img"></i>请求参数错误');
            } else if (queryData.verify == "5") {
                //               sh1003Val = "银行卡不支持";
                $(".identity-sfyhk").html('<i class="i-bad i-img"></i>银行卡不支持');
            } else {
                //                sh1003Val = "未查得";
                $(".identity-sfyhk").html('<i class="i-bad i-img"></i>未查得');
            }
        } else if (queryData.msg) {
            //            sh1003Val = queryData.msg;
            $(".identity-sfyhk").html('<i class="i-bad i-img"></i>' + queryData.msg);
        }
        /*        createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": sh1003Val
                }];
                createTableTitle = identifyMap[1].name;*/
    } else if (queryData.interId == "2001" && queryData.channelId == "suanhua") { // 算话多重身份
        /*  if (queryData.code == "0000") {
              if (queryData.verify == "1") {
                  identifyMap[6].data[3].value = "查得";
              } else {
                  identifyMap[6].data[3].value = "未查得";
              }
          } else if (queryData.msg) {
              identifyMap[6].data[3].value = queryData.msg;
          } else {
              dangerClass.index = 3;
              identifyMap[6].data[3].value = "查询失败";
          }
          createTabelData = identifyMap[6].data;
          createTableTitle = identifyMap[6].name;
          dangerClass.theName = identifyMap[6].value;*/
    } else if (queryData.interId == "8005" && queryData.channelId == "suanhua") { // 算话逾期信息
        /*if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                identifyMap[7].data[3].value = "查得";
            } else {
                identifyMap[7].data[3].value = "未查得";
            }
        } else {
            dangerClass.index = 3;
            identifyMap[7].data[3].value = "查询失败";
        }
        createTabelData = identifyMap[7].data;
        createTableTitle = identifyMap[7].name;*/
    } else if (queryData.interId == "8006" && queryData.channelId == "suanhua") { // 算话贷款信息
        /*if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                identifyMap[8].data[3].value = "查得";
            } else {
                identifyMap[8].data[3].value = "未查得";
            }
        } else if (queryData.msg) {
            identifyMap[8].data[3].value = queryData.msg;
        } else {
            dangerClass.index = 3;
            identifyMap[8].data[3].value = "查询失败";
        }
        createTabelData = identifyMap[8].data;
        createTableTitle = identifyMap[8].name;
        dangerClass.theName = identifyMap[8].value;*/
    } else if (queryData.interId == "5003" && queryData.channelId == "suanhua") { // 算话贷款信息
        /* if (queryData.code == "0000") {
             if (queryData.verify == "1") {
                 identifyMap[9].data[3].value = "查得";
             } else {
                 identifyMap[9].data[3].value = "未查得";
             }
         } else if (queryData.msg) {
             identifyMap[9].data[3].value = queryData.msg;
         } else {
             dangerClass.index = 3;
             identifyMap[9].data[3].value = "查询失败";
         }
         createTabelData = identifyMap[9].data;
         createTableTitle = identifyMap[9].name;
         dangerClass.theName = identifyMap[9].value;*/
    } else {
        /*createTabelData = [{
            "title": "接口查询",
            "value": "查询失败"
        }];
        createTableTitle = "未知接口";*/
    }

    if (queryData.next == 0) {
        $localLoading.hide();
    }
    /*    if (step == 0) {*/
    //    createTable(createTabelData, $tparent, createTableTitle, dangerClass);
    /*    } else {
            insertTr(trData, $identifyResultBox, dangerClass);
        }*/

}

// 拼接数组value
function joinArrayValue(tdata) {
    var av = '';
    for (var i = 0, l = tdata.length; i < l; i++) {
        if (isArray(tdata[i].value)) {
            av += tdata[i].value.join();
        } else {
            av += tdata[i].value;
        }
    }

    return av;
}

// 重置验证结果
function resetIndentify() {
    $("#identity-result_box>.identity-box").hide();
    $(".indentity-value").text("");
    $(".identity-yhkbox").hide();
    $(".identity-rlbox").hide();
    $(".identity-blacklist-box").hide();
    $(".identity-blxx-box").hide();
    $(".fourth-identity-box h3").html("&nbsp;");
    $(".rate-jiaocha").attr("src", "./img/jiaocha_icon.png");
    $(".rate-yiban").attr("src", "./img/yiban_icon.png");
    $(".rate-lianghao").attr("src", "./img/lianghao_icon.png");
    $(".rate-youxiu").attr("src", "./img/youxiu_icon.png");
    $(".rate-jihao").attr("src", "./img/jihao_icon.png");
}

// 创建展示表格
function createTable(tdata, $tparent, title, dangerClass) {
    var tbableHtml = `
<div class="row">
    <table class="table">
    <caption>
        <div class="line"></div>
        <div class="title">${title}</div>
    </caption>
    <tbody id="${dangerClass.theName}-tbody">`;
    for (var i = 0, l = tdata.length; i < l; i++) {
        var theValue = '';
        if (isArray(tdata[i].value)) { // 数组
            for (var n = 0; n < tdata[i].value.length; n++) {
                theValue += '<p>' + tdata[i].value[n] + '</p>';
            }
        } else {
            theValue = tdata[i].value;
        }
        if (i % 2 == 0) {
            tbableHtml += `
        <tr>
            <td width="200" class="td_title">${tdata[i].title}</td>`;
            if (dangerClass && i == dangerClass.index && dangerClass.className != "") {
                tbableHtml += `<td class="${dangerClass.className}" width="300">${theValue}</td>`;
            } else {
                tbableHtml += `<td id="${dangerClass.theName}-tb_${i}" width="300">${theValue}</td>`;
            }

        } else {
            tbableHtml += `
            <td width="200" class="td_title">${tdata[i].title}</td>`;
            if (dangerClass && i == dangerClass.index && dangerClass.className != "") {
                tbableHtml += `<td class="${dangerClass.className}" width="300">${theValue}</td></tr>`;
            } else {
                tbableHtml += `<td width="300">${theValue}</td></tr>`;
            }

        }
    }
    tbableHtml += '</tbody></table></div>';

    $tparent.append(tbableHtml);
}

// 往查询表格插入数据
function insertTr(tdata, $tparent, dangerClass) {
    var tbableHtml = '';
    var i = 0;
    for (var l = tdata.length; i < l; i++) {
        var theValue = tdata[i].value;
        if (theValue == "null" || theValue == null) {
            theValue = "";
        }
        if (i % 2 == 0) {
            tbableHtml += `
        <tr>
            <td width="200" class="td_title">${tdata[i].title}</td>`;
            if (dangerClass && i == dangerClass.index && dangerClass.className != "") {
                tbableHtml += `<td class="${dangerClass.className}" width="300">${tdata[i].value}</td>`;
            } else {
                tbableHtml += `<td width="300">${tdata[i].value}</td>`;
            }

        } else {
            tbableHtml += `
            <td width="200" class="td_title">${tdata[i].title}</td>`;
            if (dangerClass && i == dangerClass.index && dangerClass.className != "") {
                tbableHtml += `<td class="${dangerClass.className}" width="300">${tdata[i].value}</td></tr>`;
            } else {
                tbableHtml += `<td width="300">${tdata[i].value}</td></tr>`;
            }

        }


    }

    if (i % 2 == 1) { // 数据个数为基数 补一格
        tbableHtml += `<td width="200" class="td_title"></td><td width="300"></td></tr>`;
    }

    $tparent.find("tbody").append(tbableHtml);
}

// 修改局部lodaing
function setLocalLoading(localTiele) {
    $localLoading.find("div.title").text(localTiele);
}

// 根据套餐增减除银行卡输入框
function addDelteBankInput() {
    if (pkId == "003") {
        if (!($("#back-card_box")[0])) {
            var backCardBoxHtml = '<div id="back-card_box" class="form-group"><div id="card-dynamic_tips" class="dynamic-tips" style="display: none;"><p></p></div><label for="inputPhone" class="col-sm-2 control-label">银行卡号</label><div class="col-sm-6"><input id="bank-card" type="text" value="" class="form-control Validform_error" datatype="n13-19" errormsg="银行卡号错误！" nullmsg="请填写信息！"></div><div class="col-sm-3"><div class="Validform_checktip Validform_wrong">请填写信息！</div></div></div>';
            $userIdBox.after(backCardBoxHtml);
        }
    } else if (pkId == "002") {
        if (($("#back-card_box")[0])) {
            $("#back-card_box").remove();
        }
    }
    infoFormValid = $("#query-info_form").Validform({
        tiptype: 2
    });
}

function getscoreMap() {
    $.post(scoreIndexServlet, {}, function(sdata) {
        if (sdata.code == "0000" && sdata.data && sdata.data != null && sdata.data != "null" && sdata.data != "undefined" && sdata.data.length > 0) {
            scoreMap = sdata.data;
        }
    });
}

// 初始化查询识别
function initIdentify() {
    var $idDynamicTips = $("#id-dynamic_tips");
    var $cardDynamicTips = $("#card-dynamic_tips");
    var $phoneDynamicTips = $("#phone-dynamic_tips");
    initIdentifyVal();

    $setMealSpan4Box.on("click", selectSetMeal);
    /* $setMealButton.on("click", function() {

         commonView.showStep("verify1");
     });*/
    $("#identify-submit").on("click", function() {
        $('#loding').modal('show');
        fillInfo();
        queryIdentify();

    });
    bindEventAgain();
    spinner.spin($localLoading.find("tbody")[0]);
    $userId.on("focus", function() {
        $idDynamicTips.show();
    });
    $userPhone.on("focus", function() {
        $phoneDynamicTips.show();
    });
    $bankCard.on("focus", function() {
        $cardDynamicTips.show();
    });
    $userId.on("keyup", function() {
        var thisVal = $(this).val();
        thisVal = thisVal.replace(/[^0-9|X|x]/g, "");
        thisVal = thisVal.replace("x", "X");
        $(this).val(thisVal);
        if (thisVal.length < 7) {
            $idDynamicTips.text(thisVal);
        } else if (thisVal.length > 6 && thisVal.length < 15) {
            $idDynamicTips.text(thisVal.replace(/(\d{6})/, "$1  "));
        } else if (thisVal.length > 14) {
            $idDynamicTips.text(thisVal.replace(/(\d{6})(\d{8})/, "$1  $2  "));
        }
    });
    $bankCard.on("keyup", function() {
        var thisVal = $(this).val();
        if (thisVal.length < 5) {
            $cardDynamicTips.text(thisVal);
        } else if (thisVal.length > 4 && thisVal.length < 9) {
            $cardDynamicTips.text(thisVal.replace(/(\d{4})/, "$1  "));
        } else if (thisVal.length > 8 && thisVal.length < 13) {
            $cardDynamicTips.text(thisVal.replace(/(\d{4})(\d{4})/, "$1  $2  "));
        } else if (thisVal.length > 12) {
            $cardDynamicTips.text(thisVal.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  "));
        }

    });
    $userPhone.on("keyup", function() {
        var thisVal = $(this).val();
        if (thisVal.length < 4) {
            $phoneDynamicTips.text(thisVal);
        } else if (thisVal.length > 3 && thisVal.length < 8) {
            $phoneDynamicTips.text(thisVal.replace(/(\d{3})/, "$1  "));
        } else if (thisVal.length > 8) {
            $phoneDynamicTips.text(thisVal.replace(/(\d{3})(\d{4})/, "$1  $2  "));
        }
    });
    $userId.on("blur", function() {
        $idDynamicTips.hide();
    });
    $bankCard.on("blur", function() {
        $cardDynamicTips.hide();
    });
    $userPhone.on("blur", function() {
        $phoneDynamicTips.hide();
    });

    $("#info-reset").on("click", function() {

        $Validform.resetForm();
        $("#id-dynamic_tips").text("");
        if ($("#card-dynamic_tips")[0]) {
            $("#card-dynamic_tips").text("");
        }

        $("#phone-dynamic_tips").text("");
    });
    getscoreMap();
}
// 需要每次切换到查询时再次绑定的事件
function bindEventAgain() {
    $("a[data-value=verify1]").on("click", function() {
        commonView.showStep("verify0");
        step = 0;
    });
}

module.exports = {
    initIdentify: initIdentify,
    bindEventAgain: bindEventAgain,
    createTable: createTable,
    identifyMap: identifyMap,
    processingData: processingData,
    setPkId: setPkId,
    resetIndentify: resetIndentify
};